package util.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wq.cycling.R;

import java.util.ArrayList;

import bean.EntityForSimple;

/**
 * 围栏Adapter
 * author：wangqi on 2017/4/26 17:48
 * email：773630555@qq.com
 */

public class RallAdapter extends BaseAdapter {

	private Activity mContext;
	private LayoutInflater mInflater;
	private ArrayList<EntityForSimple> mDatas;
	public RallAdapter(Activity context, ArrayList<EntityForSimple> mDatas) {
		mContext = context;
		this.mDatas = mDatas;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.listview_rall_item,null);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.iv_in.setImageResource(mDatas.get(position).isInalarm()?R.mipmap.rall_checked:R.mipmap.rall_unchecked);
		holder.iv_out.setImageResource(mDatas.get(position).isOutalarm()?R.mipmap.rall_checked:R.mipmap.rall_unchecked);
		holder.iv_delete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				delete.listener(position);
			}
		});
		holder.tv_rallname.setText(mDatas.get(position).getName());
		holder.tv_rallradius.setText(mContext.getResources().getString(R.string.rall_radius)+mDatas.get(position).getRadius()+mContext.getResources().getString(R.string.mi));
		holder.tv_address.setText(mDatas.get(position).getAddress());
		return convertView;
	}

	private static class ViewHolder {
		public ImageView iv_delete,iv_in,iv_out;
		public TextView tv_rallname,tv_rallradius,tv_address;
		ViewHolder(View view) {
			iv_delete = ((ImageView) view.findViewById(R.id.iv_delete));
			iv_in = ((ImageView) view.findViewById(R.id.iv_in));
			iv_out = ((ImageView) view.findViewById(R.id.iv_out));
			tv_rallname = ((TextView) view.findViewById(R.id.tv_rallname));
			tv_rallradius = ((TextView) view.findViewById(R.id.tv_rallradius));
			tv_address = ((TextView) view.findViewById(R.id.tv_address));

		}
	}
	
	public static interface MyItemDelete{
		void listener(int item);
	}
	
	private MyItemDelete delete;
	
	public void setDeleteListener(MyItemDelete delete){
		this.delete = delete;
	}
}
